title: "Wiki: ggplot2"
author: "Simon Sacher"
date: "Nov 4 2025"
output:
    html_document: default
    pdf_document: default
    word_document: default

Wiki: ggplot2

library(ggplot2)

# open x11 interactive window session and plot ggplot output
# Note: will close anyway after execution from Rmd
# x11()
library(ggplot2)
x <- data.frame(x=1, y=1, label="ggplot2 intro @ EAGLE")
ggplot(data=x, aes(x=x, y=y)) + geom_text(aes(label=label), size=15)

x <- data.frame(x=1, y=1, label="ggplot2 intro @ EAGLE")
ggplot(data=x, aes(x=x, y=y)) + geom_text(aes(label=label), size=15)

# creating dummy data
x1 <- rnorm(1000, 0, 1)
x2 <- rnorm(1000, 5, 10)
x3 <- rep(c("catA", "catB", "catB", "catC", "catC", "catC"), 200)[1:1000]
x4 <- factor(rep(c("yes", "no"), 500))
df <- data.frame(a=x1, b=x2, c=x3, d=x4)

All plots

ggplot(df, aes(a,b, color=c)) + geom_point(alpha=0.5) + labs(title="first plot", x="xaxis \n and new line")

ggplot(df, aes(a)) + geom_histogram(color="white", binwidth = 1)

ggplot(df, aes(a)) + geom_density()

# combined
ggplot(
  df
) + geom_histogram(
  aes(a, after_stat(density), fill="blue", color="darkgrey")
) + geom_density(
  aes(a, after_stat(density)), colour="yellow" 
) + geom_rug(aes(a))
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# plot just count statistics 
ggplot(df, aes(c, color=c)) + geom_point(stat="count", size=4)

# flipped bar chart
ggplot(df) + geom_bar(aes(c)) + coord_flip()

# container bar chart
ggplot(df, aes(d, fill=c)) + geom_bar(position="dodge") + scale_fill_grey()

# boxplot
ggplot(df, aes(d, a)) + geom_boxplot() + geom_jitter(alpha=.5, width=.3, color="blue")

# boxplot with cut continuous values
ggplot(
  df, aes(a, b)
) + geom_boxplot(
  aes(group=cut_width(a, .5)), outlier.alpha=.1
) + geom_jitter(
  width=.02, size=0.7, alpha=.5, color="blue"
)

# facet plot
ggplot(df, aes(c)) + geom_bar() + facet_grid(d ~ .)

# 2d density
ggplot(df, aes(a,b)) + geom_point(size=1) + geom_density2d()

scatterplot with hexbins

library(hexbin)
# scatterplot with hexbins
ggplot(df, aes(a,b)) + geom_hex(bins=30)

adding regression line

# adding regression line
ggplot(df, aes(a, b)) + geom_point() + geom_smooth(method=lm)
## `geom_smooth()` using formula = 'y ~ x'

Themes

storing a plot to var and plotting with theme

a <- ggplot() + geom_point(data=df, aes(a, b, colour=c))

a + theme_bw()

# custom theme - not working on my machine
ggplot() + geom_point(data=df, aes(a, b, colour=c)) + facet_grid(a~b) + ggtitle(
    "my chart"
) + theme(
    plot.title=element_text(angle=1, size=22, colour="hotpink")
) + scale_colour_discrete(name="type")

plotting with grey theme

# same plot, grey theme
a + theme_grey()

theme methods

theme_get()
theme_set(theme_grey())
theme_get()
theme_set(theme_bw())

# options: theme_light, theme_dark, theme_minimal, ...

Steigerwald examples

#replace with your data path
df <- read.csv("~/r-projects/intro_to_programming/data/Steigerwald.csv", sep=";")

ggplot(df, aes(x=L8.ndvi, y=L8.savi)) + geom_point()

# adding color fade and smoothed lines
ggplot(df, aes(x=L8.ndvi, y=L8.savi, colour=SRTM)) + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?

# split by landcover class
ggplot(df, aes(x=L8.ndvi, y=L8.savi, colour=SRTM)) + geom_point() + geom_smooth() + facet_wrap(~LCname)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?
## The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?
## The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?
## The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?
## The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?
## The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?
## The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?
## The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?

savi per landcover

ggplot() + geom_point(data=df, aes(LCname, L8.savi, colour=SRTM))

ggplot(df, aes(x=LCname, y=L8.savi)) + geom_boxplot(
    alpha=.5
) + geom_point(
    aes(colour=SRTM), alpha=.7, size=1.5, position=position_jitter(width=0.25, height=0)
)

other examples

ggplot(df, aes(x=LCname, y=L8.savi, colour=SRTM)) + geom_jitter()

ggplot(df, aes(x=LCname, y=L8.savi)) + geom_violin()

ggplot(df, aes(x=TimeScan.NDVIavg, fill=LCname)) + geom_density(alpha=0.3)

combine geometries

ggplot(df, aes(x=LCname, y=L8.savi, colour=SRTM)) + geom_jitter(aes(alpha=SRTM, size=TimeScan.NDVIsd, colour=L8.ndvi)) + geom_boxplot(alpha=0.5)
## Warning: The following aesthetics were dropped during statistical transformation: colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical variable into a factor?

# cover image
ggplot(df, aes(x=L8.ndvi, y=L8.savi)) + geom_point(aes(colour=LCname), size=2) + facet_grid(. ~ LCname)

export as pdf_document

pdf("landcover_sv_L8savi_ndvi.pdf", width=12, height=4)
ggplot(df, aes(x=L8.ndvi, y=L8.savi)) + geom_point(aes(colour=LCname), size=2) + facet_grid(. ~ LCname)

dev.off()
## png 
##   2